/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.entity;

import com.scalia.meteocal.control.PasswordEncrypter;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 *
 * @author gabriele
 */
@Entity(name = "USERS")
@NamedQueries({
        @NamedQuery(name = User.findAll, query = "SELECT u FROM USERS u"),
        @NamedQuery(name = User.findAllExceptOne, query = "SELECT u FROM USERS u WHERE u <> :exceptUser"),
        @NamedQuery(name = User.findAllUsernames, query = "SELECT u.username FROM USERS u"),
        @NamedQuery(name = User.findAllUsernamesExceptOne, query = "SELECT u.username FROM USERS u WHERE u <> :exceptUser"),
        @NamedQuery(name = User.findAllPartecipantsToEvent, query = "SELECT i.user FROM INVITATION i WHERE i.event = :event and i.accepted = true"),
})

public class User implements Serializable {

    public static final String findAll = "findAll";
    public static final String findAllExceptOne = "findAllExceptOne";
    public static final String findAllUsernames = "findAllUsernames";
    public static final String findAllUsernamesExceptOne = "findAllUsernamesExceptOne";
    public static final String findAllPartecipantsToEvent = "findAllPartecipantsToEvent";
    private static final long serialVersionUID = 1L;
    
    @Id
    @NotNull(message = Messages.NOT_EMPTY)
    private String username;
    
    @Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
            message = "Invalid email")
    @NotNull(message = Messages.NOT_EMPTY)
    private String email;
    
    @NotNull(message = Messages.NOT_EMPTY)
    private String password;
    
    @NotNull(message = Messages.NOT_EMPTY)
    private String groupName;
    
    @NotNull(message = Messages.NOT_EMPTY)
    private String name;
    
    @NotNull(message = Messages.NOT_EMPTY)
    private String surname;

    private String city;

    private String state;

    private String address;
    
    @Pattern(regexp="([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\\d]{7})", message="Invalid phone number")
    private String telephone;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = PasswordEncrypter.encryptPassword(password);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + Objects.hashCode(this.username);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (!Objects.equals(this.username, other.username)) {
            return false;
        }
        return true;
    }
}
